package com.loong.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.loong.blog.mapper.ArticleMapper;
import com.loong.blog.model.dto.ArticleDTO;
import com.loong.blog.model.map.ArticleAndDetailMap;
import com.loong.blog.model.po.ArticleDetailPO;
import com.loong.blog.model.po.ArticlePO;
import com.loong.blog.service.ArticleService;
import com.loong.blog.settings.ArticleSettings;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * @author chenglong
 * @date 2025/5/9 15:58
 */
@Service
@EnableTransactionManagement
@RequiredArgsConstructor
public class ArticleServiceImpl implements ArticleService {

    private final ArticleMapper articleMapper;
    private final ArticleSettings articleSettings;

//    public ArticleServiceImpl(ArticleMapper articleMapper) {
//        this.articleMapper = articleMapper;
//    }

    @Override
    public List<ArticlePO> queryTopArticle() {
        Integer  lowRead = articleSettings.getLowRead();
        Integer  topRead = articleSettings.getTopRead();
        return articleMapper.topSortByReadCount(lowRead, topRead);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addArticle(ArticleDTO articleDTO) {
        ArticlePO articlePO = new ArticlePO();
        articlePO.setTitle(articleDTO.getTitle());
        articlePO.setSummary(articleDTO.getSummary());
        articlePO.setCreateTime(LocalDateTime.now());
        articlePO.setUpdateTime(LocalDateTime.now());
        articlePO.setReadCount(new Random().nextInt(200));
        articlePO.setUserId(new Random().nextInt(5000));
        int addArticle = articleMapper.insertArticle(articlePO);

        ArticleDetailPO articleDetailPO = new ArticleDetailPO();
        articleDetailPO.setArticleId(articlePO.getId());
        articleDetailPO.setContent(articleDTO.getContent());
        int addArticleDetail = articleMapper.insertArticleDetail(articleDetailPO);

        return (addArticle + addArticleDetail) == 2;
    }

    @Override
    public ArticleDTO queryById(Integer id) {
        ArticleAndDetailMap mapper = articleMapper.selectArticleAndDetail(id);
        ArticleDTO articleDTO = BeanUtil.toBean(mapper, ArticleDTO.class);
        return articleDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean editArticle(ArticleDTO articleDTO) {
        ArticlePO articlePO = new ArticlePO();
        articlePO.setTitle(articleDTO.getTitle());
        articlePO.setSummary(articleDTO.getSummary());
        articlePO.setId(articleDTO.getId());
        articlePO.setUpdateTime(LocalDateTime.now());
        int editArticle = articleMapper.updateArticle(articlePO);

        ArticleDetailPO articleDetailPO = new ArticleDetailPO();
        articleDetailPO.setArticleId(articleDTO.getId());
        articleDetailPO.setContent(articleDTO.getContent());
        int editArticleDetail = articleMapper.updateArticleDetail(articleDetailPO);

        return (editArticle + editArticleDetail) == 2;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeArticle(List<Integer> idList) {
        int article = articleMapper.deleteArticle(idList);
        int detail = articleMapper.deleteArticleDetail(idList);
        return (article + detail) == 2;
    }

    @Override
    public String queryTop20Content(Integer id) {
        ArticleDetailPO articleDetailPO = articleMapper.selectArticleDetailByArticleId(id);
        if (articleDetailPO == null) {
            return "无内容";
        }
        String top20Content = null;
        if (StringUtils.hasText(articleDetailPO.getContent())) {
            top20Content = articleDetailPO.getContent().substring(0, 20);
        }
        return top20Content;
    }
}
